<?php
//配合page.php
class page {
	public $db_host;
	public $db_user;
	public $db_pwd;
	public $db_name;
	public $db_char;
	
	public $page ="";
	public $totalpage ="";
	public $keyword ="";
	public $type="";
	public $pid="";
	
	//连接数据库参数
	function __construct($db_host,$db_user,$db_pwd,$db_name,$db_char){
		$this->db_host=$db_host;
		$this->db_user=$db_user;
		$this->db_pwd=$db_pwd;
		$this->db_name=$db_name;
		$this->db_char=$db_char;
		if($db_name==''){
			echo "数据库的库名没有传过来";
		}
	}
	//分页实体数据
	function listData($table,$pagesize="5",$type="",$keyword="",$pid=""){
		$this->page=$_REQUEST['page']?$_REQUEST['page']:1;
		$this->type=$type;
		$this->keyword=$keyword;
		$this->pid=$pid?$pid:0;
		if($pid=="all"){
			$where="";
		}else{
			$where="where user.pid=".$this->pid;
			$and="user.pid=".$this->pid." and";
		}
		$link = mysqli_connect($this->db_host,$this->db_user,$this->db_pwd);
		mysqli_select_db($link,$this->db_name);
		mysqli_set_charset($link,$this->db_char);
		if($type=='' || $keyword==''){
			$sql1 = "select count(*) from $table $where";
			$re= mysqli_query($link,$sql1);
			$rowcount = mysqli_fetch_assoc($re);
			$this->totalpage = ceil($rowcount["count(*)"]/$pagesize);
			$start=($this->page-1)*$pagesize;
			$sql = "select * from $table $where limit $start,$pagesize";
//			echo $sql;
		}else{
			$type=$this->type;
			$keyword=$this->keyword;
			$sql1 = "select count(*) from $table where $and $type like '%{$keyword}%'";
			$re= mysqli_query($link,$sql1);
			$rowcount = mysqli_fetch_assoc($re);
			$this->totalpage = ceil($rowcount["count(*)"]/$pagesize);
			$start=($this->page-1)*$pagesize;
			$sql = "select * from $table where $and $type like '%{$keyword}%' limit $start,$pagesize";
//			echo $sql;
		}
		$re= mysqli_query($link,$sql);
		$arr =array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[]=$row;
		}
		mysqli_free_result($re);
		mysqli_close($link);
		return $arr;
	}
	
	//分页链接
	function showPage(){
		$url=substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1);
		$top=$this->page<2?1:$this->page-1;
		$next=$this->page+1;
		$totalpage = $this->totalpage;
		$type = $this->type;
		$keyword = $this->keyword;
		$pid=$this->pid;
		if($this->page>=$this->totalpage){
			$this->page=$this->totalpage;
			$next=$this->totalpage;
		}
		return "<form action='#' method='get'>
				<a href='{$url}?page=1&type={$type}&keyword={$keyword}&pid={$pid}'>首页</a>
				<a href='{$url}?page={$top}&type={$type}&keyword={$keyword}&pid={$pid}'>上一页</a>
				<a href='{$url}?page={$next}&type={$type}&keyword={$keyword}&pid={$pid}'>下一页</a>
				<a href='{$url}?page={$totalpage}&type={$type}&keyword={$keyword}&pid={$pid}'>尾页</a>
				&nbsp;&nbsp;<span>当前是第{$this->page}页</span>
				&nbsp;&nbsp;<span>总共{$totalpage}页</span>
				<input type='hidden' name='type' value='{$type}'/>
				<input type='hidden' name='keyword' value='{$keyword}'/>
				<input type='hidden' name='pid' value='{$pid}'/>
				跳转至<input type='text' name='page' />
				<input type='submit' value='跳转' />
				</form>";
	}
}
?>